//自动截屏
var pageCreator = require('../phantomLib/pageCreator');

//相对于executeAction.js的位置
var renderDir = './public/screenshot/';
/**
 * @param arg
 *  url 截屏链接
 *
 * @param callback
 *  picUrl 截屏图片存放地址
 */
module.exports = function (arg,callback) {
  var url = arg.url;

  var myPage = pageCreator();
  myPage.viewportSize = {
    width:640,
    height:1100
  };

  var name = 'img'+Date.now() + '.jpg';

  myPage.open(url, function (status) {

    if(status === 'success'){

      myPage.evaluate(function () {

        var maxHeight = parseInt(getComputedStyle(document.querySelector('html')).height);

        [].forEach.call(document.querySelectorAll('.app-module'), function (module) {
          var top = module.offsetTop;
          if(top > maxHeight){
            module.remove();
          }
        });

      });

      setTimeout(function () {

        myPage.render(renderDir + name,{
          format:'jpeg',
          quality:85
        });

        callback(name);
        myPage.close();

      },20);

    }else{

      callback(false)

      myPage.close();
    }
  });
};